package com.hxs.learn.service.core;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.hxs.learn.common.entity.TreeJson;
import com.hxs.learn.common.page.Page;
import com.hxs.learn.common.page.PageHelper;
import com.hxs.learn.common.page.PageWrapper;
import com.hxs.learn.common.result.Result;
import com.hxs.learn.common.web.LoginInfo;
import com.hxs.learn.constants.core.sql.SqlColConts;
import com.hxs.learn.entity.core.SysRoleMenuPermission;
import com.hxs.learn.mapper.core.SysRoleMenuPermissionMapper;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *
 * @author huxinsheng
 * @data 2018-04-24
 */
@Service
public class SysRoleMenuPermissionService {
    private Logger                            logger = LoggerFactory.getLogger(SysRoleMenuPermissionService.class);
    private final SysRoleMenuPermissionMapper roleMenuPermissionMapper;

    @Autowired
    public SysRoleMenuPermissionService(SysRoleMenuPermissionMapper roleMenuPermissionMapper) {
        this.roleMenuPermissionMapper = roleMenuPermissionMapper;
    }

    /**
     * 查询所有
     *
     * @return List<SysRoleMenuPermission>
     */
    public List<SysRoleMenuPermission> findAllSysRoleMenuPermission() {
        return roleMenuPermissionMapper.selectList(new EntityWrapper<>(new SysRoleMenuPermission()));
    }

    /**
     * 按主键查询
     *
     * @param id 主键id
     * @return SysRoleMenuPermission
     */
    public SysRoleMenuPermission findById(Integer id) {
        return roleMenuPermissionMapper.selectById(id);
    }

    /**
     * 分页查询
     *
     * @param page 分页对象
     * @param sysRoleMenuPermission 分页条件对象
     * @return PageWrapper<SysRoleMenuPermission>
     */
    public PageWrapper<SysRoleMenuPermission> findSysRoleMenuPermissionPageData(
            Page page, SysRoleMenuPermission sysRoleMenuPermission) {
        return PageHelper.getPageData(roleMenuPermissionMapper, page, sysRoleMenuPermission);
    }

    /**
     *     按主键删除
     *
     *     @param id 主键id
     */
    @Transactional(rollbackFor = Exception.class)
    public Result removeById(Integer id) {
        roleMenuPermissionMapper.deleteById(id);

        return Result.ok();
    }

    /**
     * 保存
     *
     * @param sysRoleMenuPermission
     */
    @Transactional(rollbackFor = Exception.class)
    public Result save(SysRoleMenuPermission sysRoleMenuPermission) {
        roleMenuPermissionMapper.insert(sysRoleMenuPermission);

        return Result.ok();
    }

    @Transactional(rollbackFor = Exception.class)
    public Result saveRoleMenuPermission(LoginInfo userInfo, String roleId, String menuId, String permissionId) {
        String[] permissionIds = StringUtils.split(permissionId, ",");

        if (ArrayUtils.isEmpty(permissionIds)) {
            return Result.error("该角色菜单未添加任何权限");
        }

        EntityWrapper<SysRoleMenuPermission> wrapper = new EntityWrapper<>();

        wrapper.eq(SqlColConts.ROLE_ID, roleId).and().eq(SqlColConts.MENU_ID, menuId);

        // 先删除之前关联的菜单数据
        roleMenuPermissionMapper.delete(wrapper);

        List<SysRoleMenuPermission> pojos = new ArrayList<>(permissionIds.length);

        for (String str : permissionIds) {
            SysRoleMenuPermission pojo    = new SysRoleMenuPermission();
            String[]              idSigns = StringUtils.split(str, "\\|");

            pojo.setPermissionId(idSigns[0]);
            pojo.setSign(idSigns[1]);
            pojo.setMenuId(menuId);
            pojo.setRoleId(roleId);
            pojo.setCreateTime(new Date());
            pojo.setCreator(userInfo.getLoginName());
            pojos.add(pojo);
        }

        roleMenuPermissionMapper.insertAll(pojos);

        return Result.ok();
    }

    public List<TreeJson<Object>> getRoleMenuPermissionTreeJson(String roleId, String menuId) {
        return roleMenuPermissionMapper.selectByRoleIdAndMenuId(roleId, menuId);
    }

    public List<SysRoleMenuPermission> findByUserId(String userId) {
        return roleMenuPermissionMapper.selectByUserId(userId);
    }
}


//~ Formatted by Jindent --- http://www.jindent.com
